<template>
  <router-view/>
  <div class="container">
    <div class="login-wrapper">
      <div class="header">登录</div>
      <div class="form-wrapper">
        <input type="text" name="username" placeholder="请输入账号" class="input-item" v-model="username">
        <input type="password" name="password" placeholder="请输入密码" class="input-item" v-model="password">
        <div class="btn" @click="login">登录</div>
      </div>
    </div>
  </div>
</template>

<script>

import axios from "axios";

export default {
  name: "my-vue-app_Login",
  data() {
    return {
      username: '',
      password: '',
      //用于存储错误信息
      loginError: ''
    };
  },
  components: {
    // test
  },
  methods: {
    async login() {
      await axios.post('http://localhost:8085/hershel-user-service/user/login', {
        username: this.username,
        password: this.password
      })
          .then(response => {
            if (response.status === 200) {
              const data = response.data.data;
              console.log('data', data);
              const token = data.token;
              console.log('token', token);
              sessionStorage.setItem('token', token);
              //跳转到主页面
              this.$router.push('/main');
            }
          })
          .catch(error => {
            if (error.response) {
              this.loginError = error.response.data.message;
            }
            this.showError();
          });
    },
    showError() {
      if (this.loginError) {
        alert(this.loginError);
        // 重置错误信息，避免重复显示
        this.loginError = '';
      }
    }
  }
}
</script>

<style scoped>

html {
  height: 100%;
}

body {
  height: 100%;
}

.container {
  /* margin-top: 5%; */
  height: 980px;
  width: 100%;
  background-image: linear-gradient(to right, #fbc2eb, #a6c1ee);
}

.login-wrapper {
  background-color: #fff;
  width: 358px;
  height: 588px;
  border-radius: 15px;
  padding: 0 50px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

.header {
  font-size: 38px;
  font-weight: bold;
  text-align: center;
  line-height: 200px;
}

.input-item {
  display: block;
  width: 100%;
  margin-bottom: 20px;
  border: 0;
  padding: 10px;
  border-bottom: 1px solid rgb(128, 125, 125);
  font-size: 15px;
  outline: none;
}

.input-item::placeholder {
  text-transform: uppercase;
}

.btn {
  text-align: center;
  padding: 10px;
  margin: 0 auto;
  width: 100%;
  margin-top: 40px;
  background-image: linear-gradient(to right, #a6c1ee, #fbc2eb);
  color: #fff;
}

.msg {
  text-align: center;
  line-height: 88px;
}

a {
  text-decoration-line: none;
  color: #abc1ee;
}

</style>